import {Injectable,NestMiddleware} from '@nestjs/common';
import {Request, Response} from 'express';

const xml2js = require('xml2js');
const parser = new xml2js.Parser();

@Injectable()
export class XMLMiddleware implements NestMiddleware{
  use(req: Request, res: Response, next: () => void) {
    console.log('进入全局xml中间件。。。');
    if(req.headers['content-type'] && req.headers['content-type'].includes('application/xml')){
      req.on('data', mreq => {
        parser.parseString(mreq, (err,result) => {
          console.log('parseString转换后的数据',result);
          req['body'] = result;
        })
      })
    }
    next();
  }

}
